<?php
/**
*
* * 作    者：寒川<hanchuan@qiawei.com>
* 日    期：2016-09-20
* 版    本：1.0.0
* 功能说明：文章控制器。
*
**/

namespace Qwadmin\Controller;
use Qwadmin\Controller\ComController;
use Vendor\Tree;

class VersionController extends ComController {

	public function add(){
		
		$category = M('category')->field('id,pid,name')->order('o asc')->select();
		$tree = new Tree($category);
		$str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
		$category = $tree->get_tree(0,$str,0);
		$this->assign('category',$category);//导航
		$this -> display();
	}
		
	public function index($p=1){
		
	
		$p = intval($p)>0?$p:1;
		$version = M('aero_ver', null, 'DB_ADMIN_USER');
		$pagesize = 10;#每页数量
		$offset = $pagesize*($p-1);//计算记录偏移量
		$prefix = C('DB_PREFIX');
		$count = $version->count();
		$list  = $version->limit($offset.','.$pagesize)->select();	
		$page	=	new \Think\Page($count,$pagesize); 
		$page = $page->show();
        $this->assign('list',$list);	
        $this->assign('page',$page);
		$this -> display();
	}
	
	public function del(){
		
		$av_id = isset($_REQUEST['av_id'])?$_REQUEST['av_id']:false;
		if($av_id){
			if(is_array($av_id)){
				$av_id = implode(',',$av_id);
				$map['av_id']  = array('in',$av_id);
			}else{
				$map = 'av_id='.$av_id;
			}
			if(M('ver')->where($map)->delete()){
				addlog('删除版本：'.$av_id);
				$this->success('恭喜，版本删除成功！');
			}else{
				$this->error('参数错误！');
			}
		}else{
			$this->error('参数错误！');
		}

	}
	
	public function edit($av_id){
		
		$av_id = intval($av_id);
		$ver = M('ver')->where('av_id='.$av_id)->find();
		if($ver){
			$this->assign('ver',$ver);
		}else{
			$this->error('参数错误！');	
		}
		$this -> display();
	}
	
	public function update(){
		
	    $av_id = $_POST['av_id'];
		$data['av_type'] = $_POST['av_type'];
		$data['av_version'] = isset($_POST['av_version'])?$_POST['av_version']:false;
		$data['av_url'] = isset($_POST['av_url'])?$_POST['av_url']:false;
        $data['av_log'] = isset($_POST['av_log'])?$_POST['av_log']:'';
        $data['av_ismajor']=$_POST['av_ismajor'];
        $data['av_code']=isset($_POST['av_code'])?$_POST['av_code']:false;
        $data['av_channel']=$_POST['av_channel'];
        $data['av_addtime']=time();
          	
		if($av_id){
			M('ver')->data($data)->where('av_id='.$av_id)->save();
			addlog('编辑版本：'.$av_id);
			$this->success('恭喜！版本编辑成功！');
		}else{
			$addver = M('ver')->data($data)->add();
			if($addver){
				addlog('新增版本：'.$addver);
				$this->success('$_FILES["av_file_upload"]["name"]');
			}else{
				$this->error('抱歉，未知错误！');
			}
			
		}
	}
}